package util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Arat.Wu on 2018/05/24.
 */
public class Block {

    private static final Object object = new Object();

    private static final ReentrantLock lock = new ReentrantLock();

    static class Task implements Runnable {
        @Override
        public void run() {
            synchronized (object) {
                System.out.println(Thread.currentThread().getName());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Task2 implements Runnable {
        @Override
        public void run() {
            lock.lock();
            System.out.println("lock"+Thread.currentThread().getName());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 1; i <= 10; i++) {
            executorService.submit(new Task());
            executorService.submit(new Task2());

        }
    }

}
